package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.HisOrderFormDto;
import com.woniu.dto.PageDTO;
import com.woniu.exception.DIYException;
import com.woniu.model.HisOrderForm;
import com.woniu.mapper.HisOrderFormMapper;
import com.woniu.param.HisOrderFormAddParam;
import com.woniu.param.HisOrderFormParam;
import com.woniu.param.PageParam;
import com.woniu.service.IHisOrderFormService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import com.woniu.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 患者医嘱表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-11
 */
@Service
public class HisOrderFormServiceImpl extends ServiceImpl<HisOrderFormMapper, HisOrderForm> implements IHisOrderFormService {
    @Resource
    private HisOrderFormMapper hisOrderFormMapper;


    @Override
    public Result<HisOrderFormDto> addHisOrderForm(HisOrderFormAddParam hisOrderFormAddParam) {
        if (hisOrderFormAddParam==null){new DIYException(-9999,"服务器异常，hisOrderFormAddParam为空");}
        HisOrderForm hisOrderForm =new HisOrderForm();
        BeanUtils.copyProperties(hisOrderFormAddParam,hisOrderForm);
        hisOrderFormMapper.insert(hisOrderForm);
        HisOrderFormDto hisOrderFormDto = new HisOrderFormDto();
        BeanUtils.copyProperties(hisOrderForm,hisOrderFormDto);
        return Result.bulid(hisOrderFormDto);
    }

    @Override
    public Result<Boolean> deleteHisOrderForm(ArrayList<Integer> id) {
        if (id==null){new DIYException(-9999,"服务器异常，id为空");}
        int i = hisOrderFormMapper.deleteBatchIds(id);
        return Result.bulid(i==0?new Boolean(false):new Boolean(true));
    }

    @Override
    public Result<HisOrderFormDto> updateHisOrderForm(HisOrderFormParam hisOrderFormParam) {
        if (hisOrderFormParam==null){new DIYException(-9999,"服务器异常，hisOrderFormParam为空");}
        HisOrderForm hisOrderForm =new HisOrderForm();
        HisOrderFormDto hisOrderFormDto = new HisOrderFormDto();
        BeanUtils.copyProperties(hisOrderFormParam,hisOrderForm);
        BeanUtils.copyProperties(hisOrderFormParam,hisOrderForm);
        hisOrderFormMapper.updateById(hisOrderForm);
        return Result.bulid(hisOrderFormDto);
    }

    @Override
    public Result<PageDTO<HisOrderFormDto>> getAllHisOrderForm() {
        return null;
    }

    @Override
    public Result<PageDTO<HisOrderFormDto>> getAllHisOrderFormBy(HisOrderFormParam hisOrderFormParam) {
        if (hisOrderFormParam==null){new DIYException(-9999,"hisOrderFormParam为空");}
        QueryWrapper<HisOrderForm> wrapper = new QueryWrapper<>();
        if (hisOrderFormParam.getOrderFormId()!=null){
            wrapper.eq("order_form_id",hisOrderFormParam.getOrderFormId());
        }
        if (hisOrderFormParam.getPatientId()!=null){
            wrapper.eq("patient_id",hisOrderFormParam.getPatientId());
        }
        if (hisOrderFormParam.getPatientName()!=null){
            wrapper.eq("patient_name",hisOrderFormParam.getPatientName());
        }
        if (hisOrderFormParam.getOrderFormTime()!=null){
            wrapper.eq("order_form_time",hisOrderFormParam.getOrderFormTime());
        }
        if (hisOrderFormParam.getDoctorId()!=null){
            wrapper.eq("doctor_id",hisOrderFormParam.getDoctorId());
        }
        if (hisOrderFormParam.getNurseId()!=null){
            wrapper.eq("nurse_id",hisOrderFormParam.getNurseId());
        }
        if (hisOrderFormParam.getOrderFormState()!=null){
            wrapper.eq("order_form_state",hisOrderFormParam.getOrderFormState());
        }
        if (hisOrderFormParam.getDoctorName()!=null){
            wrapper.eq("doctor_name",hisOrderFormParam.getDoctorName());
        }
        if (hisOrderFormParam.getNurseName()!=null){
            wrapper.eq("nurse_name",hisOrderFormParam.getNurseName());
        }
        if (hisOrderFormParam.getOrderFormEndTime()!=null){
            wrapper.eq("order_form_end_time",hisOrderFormParam.getOrderFormEndTime());
        }
        if (hisOrderFormParam.getOrderFormEndStart()!=null){
            wrapper.eq("order_form_end_start",hisOrderFormParam.getOrderFormEndStart());
        }
        if(hisOrderFormParam.getPageParam()==null){
            PageParam pageParam = new PageParam();
            pageParam.setPage(0L);
            pageParam.setPageSize(100L);
            hisOrderFormParam.setPageParam(pageParam);
        }
        System.out.println(hisOrderFormParam);
        Page<HisOrderForm> page = page(new Page<HisOrderForm>(hisOrderFormParam.getPageParam().getPage(),hisOrderFormParam.getPageParam().getPageSize()),wrapper);
        List<HisOrderFormDto> hisOrderFormDtos = CopyBeanUtil.copyBean(page.getRecords(), HisOrderFormDto.class);
        System.out.println(hisOrderFormDtos);
        return Result.bulid(PageDTO.bulid(page,hisOrderFormDtos));
    }
}
